﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class DatabaseCheckMaxFileGrowthStateOn:IssueTypeCheckBase
    {

        public DatabaseCheckMaxFileGrowthStateOn()
        {
            IssueTypeId = 3009;
        }

        public override void Check()
        {
           
            HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


            /*
               * This rule checks if there is a specified Max Files Size limit if value is -1 growth is said to be Unrestricted..
               */

            var QryMaxGrowth = from d in dc.DatabaseFiles

                      where d.MaxSize != -1

                      select d;

            foreach (var database in QryMaxGrowth)
            {
                IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                //Create new Issue

                Issue issue = new Issue();
                issue.IssueType = issueType;
                issue.TargetId = database.DatabaseID;
                issue.Description = issueType.Description.Replace("{Database}", database.Database.DatabaseName).Replace("{MaxSize}",database.MaxSize.ToString()).Replace("{FileName}", database.File.ToString());



              


                dc.Issues.InsertOnSubmit(issue);


            }

            dc.SubmitChanges();
         
       
        }
       
    }
}
